package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.mapper.SkuMapper;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpecParamService specParamService;

    @Autowired
    private SpuService spuService;

    @Override
    public List<SkuDTO> listSkuBySid(Long sid) {
        QueryWrapper<Sku> queryWrapper = new QueryWrapper<Sku>()
                .eq("spu_id", sid);
        List<Sku> skuList = this.list(queryWrapper);
        return SkuDTO.convertEntityList(skuList);
    }

    @Override
    public List<SkuDTO> listSkuBySkuIds(List<Long> skuIds) {
        return SkuDTO.convertEntityList(this.listByIds(skuIds));
    }

    @Override
    public List<SpecParamDTO> querySpecsValues(Long sid, Boolean isSearching) {
        //根据sid获取对应商品对象
        SpuDTO spuDTO = spuService.querySpuById(sid);
        //获取三级类目
        Long cid3 = spuDTO.getCid3();
        //获取类目下的所有规格
        List<SpecParamDTO> specParamDTOS = specParamService.ListSpecParamsByCondition(cid3, null, isSearching);

        //获取spuDetail
        SpuDetailDTO spuDetailDTO = spuDetailService.queryDetailBySid(sid);
        String specification = spuDetailDTO.getSpecification();
        //将specification中的json 数据转换成map集合,其中key为规格id ,value为规格对应的值
        Map<Long,Object> map = JsonUtils.nativeRead(specification, new TypeReference<Map<Long, Object>>() {});
        specParamDTOS.forEach(specParamDTO -> specParamDTO.setValue(map.get(specParamDTO.getId())));

        return specParamDTOS;
        //根据sid查询detail
//        SpuDetailDTO spuDetailDTO = spuDetailService.queryDetailBySid(sid);
        //获取对象中的规格参数值
//        String specification = spuDetailDTO.getSpecification();
//        SpuDetail spudetail = spuDetailService.getOne(new QueryWrapper<SpuDetail>().eq("spu_id", sid).select("specification"));
//        String specification = spudetail.getSpecification();
//        ObjectMapper om = new ObjectMapper();
//        // key为specId Object为规格参数值
//        Map<String,Object> map = null;
//        try {
//            map = om.readValue(specification, Map.class);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        //创建集合用于封装数据
//        ArrayList<SpecParamDTO> list = new ArrayList<>(map.size());
//
//        Set<String> specIdSet = map.keySet();
//        Iterator<String> iterator = specIdSet.iterator();
//
//        if(isSearching == null){
//            while(iterator.hasNext()){
//                //获取规格参数id
//
//                Long specId = Long.valueOf(iterator.next());
//                //根据规格参数id找到对应的规格参数
//                SpecParam specParam = specParamService.getById(specId);
//                SpecParamDTO specParamDTO = new SpecParamDTO(specParam);
//                specParamDTO.setValue(map.get(specId.toString()));
//                list.add(specParamDTO);
//            }
//        }else {
//            while(iterator.hasNext()){
//                //获取规格参数id
//                Long specId = Long.valueOf(iterator.next());
//                //根据规格参数id找到对应的规格参数
//                QueryWrapper<SpecParam> queryWrapper = new QueryWrapper<SpecParam>()
//                        .eq("id", specId)
//                        .eq("searching", isSearching);
//                SpecParam specParam = specParamService.getOne(queryWrapper);
//
//                if(specParam != null){
//                    SpecParamDTO specParamDTO = new SpecParamDTO(specParam);
//                    specParamDTO.setValue(map.get(specId.toString()));
//                    list.add(specParamDTO);
//                }
//            }
//        }
//
//
//
//        return list;
    }


}
